package com.hoshiicloud.market.controller.market;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.market.entity.market.Banner;
import com.hoshiicloud.market.service.market.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author yangcheng
 * @version 1.0
 * @title: BannerController
 * @description: Banner图片Controller
 * @date 2019/8/6 16:11
 * @mail maiboyc@163.com
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/banner")
@Api(value = "banner图片", tags = {"banner图片管理"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class BannerController {

    // banner图片Service
    @Autowired
    private BannerService BannerService;

    /**
     * 分页查询banner图片
     * @param param 分页查询参数
     * @return Response
     */
    @ApiOperation(value = "分页查询banner图片")
    @PostMapping("/page")
    public Response page(@RequestBody Banner param) {
        try {
            log.info("分页查询banner图片");
            QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(param.getCompanyId() != null,Banner::getCompanyId,param.getCompanyId())
                    .eq(param.getPlatformId() != null, Banner::getPlatformId, param.getPlatformId())
                    .eq(param.getStoreId() != null, Banner::getStoreId, param.getStoreId())
                    .eq(param.getName() != null, Banner::getName, param.getName())
                    .eq(param.getUrl() != null, Banner::getUrl, param.getUrl())
                    .eq(Banner::getDeleted,0);
            IPage<Banner> page = new Page<>(param.getPageNumber(), param.getPageSize());
            return Response.data(BannerService.page(page, queryWrapper,param.toString()));
        } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("分页查询banner图片异常", e);
            return Response.failed("分页查询banner图片异常"+e.getMessage());
        }
    }

    /**
     * 查询banner图片
     * @param param 查询参数
     * @return Response<List<Banner>>
     */
    @ApiOperation(value = "查询banner图片")
    @PostMapping("/list")
    public Response<List<Banner>> list(@RequestBody Banner param) {
        try {
            log.info("查询banner图片");
            QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(param.getCompanyId() != null,Banner::getCompanyId,param.getCompanyId())
                    .eq(param.getPlatformId() != null, Banner::getPlatformId, param.getPlatformId())
                    .eq(param.getStoreId() != null, Banner::getStoreId, param.getStoreId())
                    .eq(param.getName() != null, Banner::getName, param.getName())
                    .eq(param.getUrl() != null, Banner::getUrl, param.getUrl())
                    .eq(Banner::getDeleted,0);
            return Response.data(BannerService.list(queryWrapper,param.toString()));
        } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("查询banner图片异常", e);
            return Response.failed("查询banner图片异常"+e.getMessage());
        }
    }

    /**
     * 根据id查询banner图片
     * @param id 主键ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询banner图片")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(BannerService.getById(id));
    }

    /**
     * 新增banner图片
     * @param param 新增参数
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增banner图片")
    public Response<Long> save(@RequestBody @Valid Banner param) {
        try {
            return Response.data(BannerService.insert(param));
        } catch (ServiceException e) {
            log.error("新增banner图片异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增banner图片异常", e);
            return Response.failed();
        }
    }

    /**
     * 修改banner图片
     * @param param 修改参数
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改banner图片")
    public Response update(@RequestBody @Valid Banner param) {
        try {
            return Response.data(BannerService.updateById(param));
        }catch (ServiceException e) {
            log.error("修改banner图片异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改banner图片异常", e);
            return Response.failed();
        }
    }

    /**
     * 删除banner图片
     * @param id 主键ID
     * @return Response
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除banner图片")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        try {
            Banner data = BannerService.getById(id);
            if(data==null){
                log.error("删除banner图片异常，无法找到banner图片记录");
                return Response.failed("删除banner图片异常，无法找到banner图片记录");
            } else {
                return Response.data(BannerService.deleteById(data));
            }
        }catch (ServiceException e) {
            log.error("删除banner图片异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("删除banner图片异常", e);
            return Response.failed("删除banner图片异常："+e.getMessage());
        }
    }
    
}
